﻿/**
//  GetPropertyDescCommand.cs
//  Marquee Moment
//  Copyright 2011 Systematix Technocrates. All rights reserved.
**/
using EDSDKLib;
using MarqueeMoment.CameraController.Camera;

namespace MarqueeMoment.CameraController.Command
{
    public class GetPropertyDescCommand : Command
    {


        private uint propertyID;

        public GetPropertyDescCommand(CameraModel model, uint propertyID)
            : base(model)
        {
            this.propertyID = propertyID;
        }


        //// Execute a command.
        public override bool execute()
        {

            uint err = EDSDK.EDS_ERR_OK;
            bool locked = false;

            //// You should do UILock when you send a command to camera models elder than EOS30D.

            if (base.model.isLegacy())
            {
                err = EDSDK.EdsSendStatusCommand(base.model.getCameraObject(), EDSDK.CameraState_UILock, 0);


                if (err == EDSDK.EDS_ERR_OK)
                {
                    locked = true;
                }
            }

            ////Get an available property list.

            if (err == EDSDK.EDS_ERR_OK)
            {
                err = getPropertyDesc(this.propertyID);

            }


            if (locked == true)
            {
                err = EDSDK.EdsSendStatusCommand(base.model.getCameraObject(), EDSDK.CameraState_UIUnLock, 0);

            }

            //// Notify Error.

            if (err != EDSDK.EDS_ERR_OK)
            {
                //// Retry when the camera replys deviceBusy.

                if ((err + EDSDK.EDS_ERRORID_MASK) == EDSDK.EDS_ERR_DEVICE_BUSY)
                {
                    base.model.notifyObservers(commandName.warn, err);

                    return false;

                }

                base.model.notifyObservers(commandName.errr, err);

            }
            return true;

        }


        private uint getPropertyDesc(uint id)
        {

            uint err = EDSDK.EDS_ERR_OK;
            EDSDK.EdsPropertyDesc propertyDesc = new EDSDK.EdsPropertyDesc();


            if (id == EDSDK.PropID_Unknown)
            {
                //// If the propertyID is invalidID,
                //// you should retry to get available property lists.
                //// InvalidID is able to be published for the models elder than EOS30D.

                if (err == EDSDK.EDS_ERR_OK)
                {
                    err = getPropertyDesc(EDSDK.PropID_AEMode);
                }
                if (err == EDSDK.EDS_ERR_OK)
                {
                    err = getPropertyDesc(EDSDK.PropID_Tv);
                }
                if (err == EDSDK.EDS_ERR_OK)
                {
                    err = getPropertyDesc(EDSDK.PropID_Av);
                }
                if (err == EDSDK.EDS_ERR_OK)
                {
                    err = getPropertyDesc(EDSDK.PropID_ISOSpeed);
                }

                return err;
            }

            //// Get available property lists.

            if (err == EDSDK.EDS_ERR_OK)
            {
                err = EDSDK.EdsGetPropertyDesc(base.model.getCameraObject(), id, out propertyDesc);
            }

            //// Stock the available property list.	

            if (err == EDSDK.EDS_ERR_OK)
            {
                base.model.setPropertyDesc(id, propertyDesc);

            }

            //// Notify updating.

            if (err == EDSDK.EDS_ERR_OK)
            {
                base.model.notifyObservers(commandName.upls, id);

            }

            return err;

        }

    }
}
